﻿
<!DOCTYPE html>
<html class="x-admin-sm">

<head>
    <meta charset="UTF-8">
    <title>创建生产计划</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
    <script src="~/js/layui/layui.js"></script>
    <link href="~/js/layui/css/layui.css" rel="stylesheet" />
    <link href="~/X-admin/css/xadmin.css" rel="stylesheet" />
    <script src="~/X-admin/js/jquery.min.js"></script>
    <script src="~/X-admin/js/xadmin.js"></script>
    <script src="~/js/framework/sysframework.js"></script>
    <script src="~/js/framework/commonjs.js"></script>
    <script src="~/js/sysweb/planjs.js"></script>
</head>
<body>
    <ul lay-bar="disabled" class="mWebBanner">
        <li class="layui-nav-item" style="float:left;">
            <button id="btn-update" type="button" name="btn_update" onclick="SavePlan(cData)" class="buttonCss">
                <i class="layui-icon" style="margin-right:3px">&#x1005;</i>保存计划
            </button>
        </li>
        <li class="layui-nav-item" style="float:left;margin-left:3%">
            <button id="btn-update" type="button" name="btn_update" onclick="OpenSale()" class="buttonCss">
                <i class="layui-icon" style="margin-right:3px">&#xe65e;</i>关联销售单
            </button>
        </li>
        <li class="layui-nav-item" style="float:left;margin-left:3%">
            <button id="btn-update" type="button" name="btn_update" onclick="ViewPlan()" class="buttonCss">
                <i class="layui-icon" style="margin-right:3px">&#xe702;</i> 往期订单
            </button>
        </li>
        <li class="layui-nav-item" style="float:left;margin-left:3%">
            <button id="btn-sync" type="button" onclick="closeWin()" class="buttonCss">
                <i class="layui-icon" style="margin-right:3px">&#x1007;</i> 关闭页面
            </button>
        </li>
    </ul>
    <div style="height:800px;overflow-y: scroll;">
    <div class="layui-card" style="margin-top:1%">
            <div class="layui-card-body" style="padding-top:4%;">
                <form id="dForm" class="layui-form layui-col-space5">
                    <div class="layui-form-item">
                        <div class="layui-form-item">

                            <div class="layui-input-group" style="float:left;">
                                <div class="layui-input-prefix">
                                    销售单号
                                </div>
                                <input type="text" id="erpOrder" name="erpOrder" class="layui-input" style="width:120%" readonly />
                            </div>

                            <div class="layui-input-group" style="float:left;margin-left:100px">
                                <div class="layui-input-prefix">
                                    关联客户
                                </div>
                                <input type="text" id="cuName" name="cuName" class="layui-input" style="width:120%">
                                <div class="layui-input-suffix" style="cursor: pointer;" onclick="chooseCustomer()">
                                    <i class="layui-icon layui-icon-more-vertical"></i>
                                </div>
                            </div>
                        </div>
                        <div class="layui-input-group" style="float:left;">
                            <div class="layui-input-prefix">
                                生产存货
                            </div>
                            <input type="text" id="proName" name="proName" class="layui-input" style="width:120%">
                            <div class="layui-input-suffix" style="cursor: pointer;" onclick="chooseProduct(1)">
                                <i class="layui-icon layui-icon-more-vertical"></i>
                            </div>
                        </div>
                        <div class="layui-input-group" style="float:left;margin-left:70px">
                            <div class="layui-input-prefix">
                                存货规格
                            </div>
                            <input type="text" id="spec" name="spec" class="layui-input"  readonly />
                        </div>
                        <div class="layui-input-group" style="float:left;margin-left:80px">
                            <div class="layui-input-prefix">
                                生产数量
                            </div>
                            <input type="text" id="number" name="number" class="layui-input" style="width:80%" required>
                        </div>
                    </div>
                    <div class="layui-form-item">

                        <div class="layui-input-group" style="float:left;">
                            <div class="layui-input-prefix">
                                半成品存货
                            </div>
                            <input type="hidden" id="cu_bcp" name="cu_bcp">
                            <input type="text" id="produce_proName" name="produce_proName" class="layui-input" style="width:120%" />
                            <div class="layui-input-suffix" style="cursor: pointer;" onclick="chooseProduct(2)">
                                <i class="layui-icon layui-icon-more-vertical"></i>
                            </div>
                        </div>

                        <div class="layui-input-group" style="float:left;margin-left:100px">
                            <div class="layui-input-prefix">
                                半成品存货规格
                            </div>
                            <input type="text" id="produce_spec" name="produce_spec" class="layui-input" readonly>
                        </div>
                    </div>

                    <div class="layui-form-item">
                   
                        <div class="layui-input-group" style="float:left;">
                            <div class="layui-input-prefix">
                                交付日期
                            </div>
                            <input type="text" id="jfDate" name="jfDate" class="layui-input demo-laydate-item" lay-options="{}" style="width:120%">
                            <div class="layui-input-suffix">
                                <i class="layui-icon layui-icon-date"></i>
                            </div>
                        </div>
                        <div class="layui-input-group" style="float:left;margin-left:70px">
                            <div class="layui-input-prefix">
                                完成日期
                            </div>
                            <input type="text" id="finishDate" name="finishDate" class="layui-input demo-laydate-item" lay-options="{}" style="width:120%">
                            <div class="layui-input-suffix">
                                <i class="layui-icon layui-icon-date"></i>
                            </div>
                            <span id="msg" style="width:250px;color:red">&nbsp;&nbsp;&nbsp;选择日期不合法（完成日期应小于交付日期）</span>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-input-group" style="float:left;">
                            <div class="layui-input-prefix">
                                生产BOM
                            </div>
                            <input type="text" id="bomCode" name="bomCode" class="layui-input" style="width:120%" readonly>
                        </div>
                        <div class="layui-input-group" style="float:left;margin-left:10%">
                            <div class="layui-input-prefix">
                                生产车间
                            </div>
                            <select id="cjCode" name="cjCode" lay-verify="required" style="width:120%"></select>

                        </div>

                        <div class="layui-input-group" style="float:left;margin-left:10%">
                            <div class="layui-input-prefix">
                                生产工厂
                            </div>
                            <select id="gcName" name="gcName" lay-verify="required" style="width:120%">
                                <option value="雄县">雄县</option>
                                <option value="喀左">喀左</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-form-item">
                
                        <div class="layui-input-group" style="float:left;">
                            <div class="layui-input-prefix">
                                预警天数
                            </div>
                            <input type="number" id="remind" name="remind" class="layui-input" style="width:80%" onblur="checkNum()">
                        </div>
                        <div class="layui-input-group" style="float:left;margin-left:10%">
                            <div class="layui-input-prefix">
                             生产标准
                            </div>
                            <select id="zhxBz" name="zhxBz" lay-verify="required" style="width:120%"></select>
                        </div>
                        <div class="layui-input-group" style="float:left;margin-left:10%">
                            <div class="layui-input-prefix">
                                生产材质
                            </div>
                            <select id="cu_caizhi" name="cu_caizhi" lay-verify="required" style="width:120%"></select>

                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-input-group" style="float:left;">
                            <div class="layui-input-prefix">
                                订单备注
                            </div>
                            <textarea type="text" id="orderMemo" name="orderMemo" class="layui-textarea" style="width:400%;" readonly></textarea>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-input-group" style="float:left;">
                            <div class="layui-input-prefix">
                                计划备注
                            </div>
                            <input type="text" id="memo" name="memo" class="layui-input" style="width:370%">
                        </div>
                    </div>

                    <input type="hidden" id="plType" name="plType">
                    <input type="hidden" id="proCode" name="proCode">
                    <input type="hidden" id="cuCode" name="cuCode">
                    <input type="hidden" id="erpRowId" name="erpRowId">
                    <input type="hidden" id="errmsg" name="errmsg"> @* BOM错误信息 *@
                    <input type="hidden" id="id" name="id">
                    <input type="hidden" id="plCode" name="plCode">
                </form>
            </div>
     </div>
        <div class="layui-card" style="margin-top:0.6%;background-color:#fff;height:350px">
            <div class="layui-card-body layui-table-body layui-table-main" id="layui-table-main" style="width:35%;float:left;">
                <table class="layui-hide" id="list" lay-filter="list"></table>  @* 工序列表 *@
            </div>
            <div class="layui-card-body layui-table-body layui-table-main" id="layui-table-main" style="width:55%;float:left;">
                <table class="layui-hide" id="bgList" lay-filter="bgList"></table>  @* 版辊列表 *@
            </div>
        </div>
    </div>
</body>
</html>

<script type="text/javascript">
    var dataId = "";
    var sign = true;  //防止重复保存

    $(function () {
        CommonData("sccj", "cjCode", "");
        CommonData("zhxBz", "zhxBz", "");
        CommonData("caizhi", "cu_caizhi", "");
        $("input[ name='remind']").val("0");  //计划预警 默认0（不预警）

        dataId = getUrlVariable("dataId");
        if (dataId == "" || dataId == "0") {
            loadGx();
            $("input[ name='plType']").val("P-BeiHuo");
        } else {
            $("input[ name='plCode']").val(dataId);
            loadPlan(dataId);
            loadPlanGx(dataId);
            loadGx();
        }
        $('#msg').hide();  // hide
        // $('#element').show();  // show
       

    });
    layui.use(function () {
        var laydate = layui.laydate;
        // 渲染
        laydate.render({
            elem: '.demo-laydate-item'
        });
    });

    function checkNum() {
        var value = $("#remind").val();
        var finishDate = $("#finishDate").val();

        if (value > 0 && finishDate == "") {
            layer.msg("未选择完成日期，系统将不做预警");
        }

        if (value % 1 === 0) {
            return true;
        } else {
            layer.msg("预警天数请输入整数");
            $("#remind").val("0");
            return false;
        }

    }

    layui.use(['form','laydate'], function () {
        var form = layui.form;
        var laydate = layui.laydate;

         // 交付日期
        laydate.render({
            elem: '#jfDate',
            done: function (value, date, endDate) { //监听日期被切换

                var finishDate = $("#finishDate").val();

                if (finishDate != "") {
                    if (value < finishDate) {
                        $('#msg').show();
                    } else {
                        $('#msg').hide();
                    }
                }
                // lay('#laydate-yezl').val(value);
                // $('.laydate-month-list').on('click', 'li', function () {//利用的事件委托
                //     $('.layui-laydate').remove()
                // })
                // overviewBaladjust();
            }
        });

        // 完成日期
        laydate.render({
            elem: '#finishDate',
            done: function (value, date, endDate) { //监听日期被切换

                var jfDate = $("#jfDate").val();

                if (jfDate!=""){
                    if (value > jfDate) {
                        $('#msg').show();
                    } else {
                        $('#msg').hide();
                    }
                }
            }
        });

        

    });

    var cData = new Array();

    //获取生产工序
    function loadGx() {
        var dUrl = "../Plan/getDataList?dataType=gxlist&condition=" + dataId;
        layui.use(['table'], function () {
            var table = layui.table, form = layui.form;
            table.render({
                elem: '#list'
                , cols: [[ //标题栏
                    { type: 'checkbox' }
                    , { field: 'cName', title: '工序名称', width: 110 }
                    , { field: 'orderNum', title: '工序序号', width:90, edit: 'text' }
                    , { field: 'MaxFlNum', title: '废料最大值', width: 90, edit: 'text' }
                ]], done: function (res, page, count) {
                    count = res.data.length;
                    var data = res.data;

                    //获取计划选中的工序  id
                    var selectIds = new Array();  
                    for (var i = 0; i < data.length; i++) {

                        gxData.find(function (value, index, gxData) {

                            if (value["cCode"] == data[i]["cCode"]) {
                                selectIds.push(i);
                            }
                        })
                    }

                    //默认选中
                    table.setRowChecked('list', {
                        type: "checkbox",
                        index: selectIds // 选中行的下标。 0 表示第一行
                    });

                    //将选中值赋给cData
                    var select = new Array();
                    var checkStatus = table.checkStatus('list');

                    for (var i = 0; i < checkStatus.data.length; i++) {
                        select.push(checkStatus.data[i]);
                    }
                    cData = select;

                }
                , url: dUrl
                , skin: 'row' //表格风格
                , even: true
                , height: 'full-220'   //固定高度-即固定表头固定第一行首行
                , page: false //是否显示分页
            });

          
            /**复选框**/
            table.on('checkbox(list)', function (obj) {

                var select = new Array();
                var checkStatus = table.checkStatus('list');
                //console.log(checkStatus)

                for (var i = 0; i < checkStatus.data.length; i++) {
                    select.push(checkStatus.data[i]);
                }
                cData = select;
                // console.log(cData)
            });

            //监听行工具事件
            table.on('edit(list)', function (obj) { //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
                var data = obj.data;
                var newNum = obj.value;
                //console.log(cData)
                //console.log(data)

                if (obj.field == "MaxFlNum") {

                    if (isNaN(data.MaxFlNum)) {
                       
                        layer.msg("输入格式错误");

                        data["MaxFlNum"] = "0";
                        $(this).val("0");
                        obj.update(data);
                    }
                    
                }

                //修改选中的工序 的 工序序号
                cData.find(function (value, index, cData) {

                    if (value["cCode"] == data["cCode"]) {
                        // cData[index]["orderNum"] = newNum;
                        cData[index][obj.field] = newNum;
                    }
                })
                //console.log(cData)
            });


        });
    }

    var gxData = new Array();
    /**
    * 加载计划工序信息
    **/
    function loadPlanGx(plCode) {
        $.ajax({
            url: "../Plan/getPlanGxByCode",
            data: { plCode: plCode },
            type: "post",
            success: function (rsl) {
                if (rsl.result) {
                    for (var i = 0; i < rsl.data.length; i++) {
                        // gxData.push(rsl.data[i]["gxCode"])
                        gxData.push({ cCode: rsl.data[i]["gxCode"], orderNum: rsl.data[i]["gxOrder"], MaxFlNum: rsl.data[i]["MaxFlNum"] })
                    }
                } else {
                    layer.msg(rsl.message);
                }
            }
        });

    }

    /**
    * 加载版辊信息
    **/
    function loadBanGun(datas) {
        layui.use(['table', 'laydate'], function () {
            var table = layui.table;
            var laydate = layui.laydate;
            var cols = table.render({
                elem: '#bgList'
                , data: JSON.parse(datas)
                , skin: 'row' //表格风格
                , even: true
                , height: 'full-220'   //固定高度-即固定表头固定第一行首行
                , page: false
                , limits: [50, 70, 100]
                , limit: 50 //每页默认显示的数量
                , cols: [[
                    { field: 'byCode', title: '版辊号', width: 100 }
                    , { field: 'byBarCode', title: '版辊条码', width: 120 }
                    , { field: 'banchang', title: '版长', width: 100 }
                    , { field: 'banzhou', title: '版周', width: 100 }
                    , { field: 'statusName', title: '状态', width: 110 }
                    , { field: 'wareHouseName', title: '仓库', width: 120 }
                ]], done: function (data) {

                }
            }).config.cols;


        });
    }

    var selectype = "";//1 选择成品 2选择半成品

    /**
    * 存货选择
    * **/
    function chooseProduct(type) {
        selectype = type;
        var pType = $("input[ name='plType']").val();
        if(type=="2"){
            let url = "../Basic/SelectInventory?isSingle=0";
            InfoOpenPage(url, "存货选择", "95%", "90%");
        }else{
            if (pType == "P-BeiHuo") {
                let url = "../Basic/SelectInventory?isSingle=0";
                InfoOpenPage(url, "存货选择", "95%", "90%");
            }
        }
        
    }
    function getChildPar(pars) {
        if (pars != undefined) {
            if (selectype == "1") {
                $("input[ name='proName']").val(pars[0].proName);
                $("input[ name='proCode']").val(pars[0].proCode);
                $("input[ name='spec']").val(pars[0].spec);
                //GetGyCode();
                GetBOMCode();
            }
            else {
                $("input[ name='produce_proName']").val(pars[0].proName);
                $("input[ name='cu_bcp']").val(pars[0].proCode);
                $("input[ name='produce_spec']").val(pars[0].spec);
            }
        }
    }


</script>

